a = int(input())
b = []
c = []
b.append(a)
a = str(a)
if int(a) <= 999 or int(a) >= 10000:
    print("0")
    exit()
for i in range(4):
    b.append(int(a[i]))
    if i != 3:
        b.append(int(a[i:i+2]))
    if i != 2:
        b.append(int(a[i:i+3]))
for i in b:
    for f in range(2, i):
        if i % f == 0:
            break
    else:
        c.append(i)
if c == [ ]:
    print("0")
else:
    print(max(c))
